package mysql

import (
	"novel-hub/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Collection struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null;comment:'用户 ID'"`
	NovelId int64 `gorm:"type:int(11);not null;comment:'小说 ID'"`
}

func (c *Collection) Collection(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collection) CollectionList(db *gorm.DB, lists []*novelrpc.CollectionList) ([]*novelrpc.CollectionList, error) {
	/*
		SELECT
		collections.id as collection_id,users.nickname,novels.title
		FROM
		collections
		LEFT JOIN users ON users.id=collections.user_id
		LEFT JOIN novels ON novels.id=collections.novel_id
	*/
	err = db.Debug().Model(c).
		Select("collections.id as collection_id,users.nickname,novels.title").
		Joins("LEFT JOIN users ON users.id=collections.user_id").
		Joins("LEFT JOIN novels ON novels.id=collections.novel_id").
		Find(&lists).Error
	return lists, err
}

func (c *Collection) FindCollectionById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(c).Error
}

func (c *Collection) CollectionDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(c).Error
}
